﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace WaiGaoQiaoWeb.Controllers
{
    public class XiYiDeptReportController : Controller
    {
        private XiYiDeptReportInfo meninfo = new XiYiDeptReportInfo();
        public ActionResult XiYiDeptReportIndex()
        {
            return View();
        }

        /// <summary>
        /// 报表导出
        /// </summary>
        /// <returns></returns>
        public ActionResult ReportOut(string Date)
        {
            return EContent(Date);
        }

        public ExcelContentResult EContent(string Date)
        {
            string[] sDate = new string[2];
            sDate = Date.Split('-');
            int yy = Convert.ToInt32(sDate[0]);
            int mm = Convert.ToInt32(sDate[1]);
            StringBuilder sBuilder = new StringBuilder();
            DataSet ds = meninfo.GetUpName(Date);
            DataSet list = meninfo.BindList(yy, mm);
            sBuilder.Append("<table cellspacing='0' cellpadding='5' rules='all' border='1'>");
            sBuilder.Append("<tr style='white-space: nowrap;'>");
            sBuilder.Append("<td colspan='" + (Convert.ToInt32(ds.Tables[0].Rows.Count) + 2).ToString() + "' style='font-size:25px; text-align:center;'>港盛公司洗衣房部门日报表(" + mm + "月)</td>");
            sBuilder.Append("</tr>");
            sBuilder.Append("<tr style='font-size:17px; white-space: nowrap;'>");
            sBuilder.Append("<td>日期</td>");
            sBuilder.Append("<td>总计</td>");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                sBuilder.Append("<td>" + ds.Tables[0].Rows[i]["DEPTNAME"] + "</td>");
            }
            sBuilder.Append("</tr>");
            foreach (DataRow item in list.Tables[0].Rows)
            {
                sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
                for (int j = 0; j < list.Tables[0].Columns.Count; j++)
                {
                    sBuilder.Append("<td>" + item[j].ToString() + "</td>");
                }
                sBuilder.Append("</tr>");
            }
            int count = list.Tables[0].Columns.Count;
            decimal[] icounts = new decimal[count - 1];
            for (int i = 0; i < list.Tables[0].Rows.Count; i++)
            {
                for (int j = 0; j < list.Tables[0].Columns.Count-1; j++)
                {
                    icounts[j] = icounts[j] + Convert.ToDecimal(list.Tables[0].Rows[i][j+1]);
                }
            }
            sBuilder.Append("<tr style='font-size:14px; white-space: nowrap;'>");
            sBuilder.Append("<td right='right'>合计</td>");
            for (int i = 0; i < icounts.Length; i++)
            {
                sBuilder.Append("<td>" + icounts[i].ToString() + "</td>");
            }
            sBuilder.Append("</tr>");
            sBuilder.Append("</table>");
            ExcelContentResult econtent = new ExcelContentResult();
            econtent.Content = sBuilder.ToString();
            econtent.filename = "XiYiDeptDayReport";
            econtent.ContentType = "application/ms-excel";
            econtent.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            return econtent;
        }
    }
}
